<?php
	class MovMsDAO{
		protected $db;

		public function __construct($db){
			$this->db = $db;
		}

		public function setDb($db){
			$this->db = $db;
		}

		public function getDb(){
			return $this->db;			
		}

		public function addMovMs($m){			
			$sql = "insert into mov_ms (mov, ms,qtde,tipo,nf,ped,clien_nome,dt_ocorrencia,obs,local,expedicao)
					values({$m->getMov()->getId()},{$m->getMs()->getId()},{$m->getQtde()},'{$m->getTipo()}','{$m->getNf()}','{$m->getPed()}','{$m->getClien()}','{$m->getDtOcorrencia()}','{$m->getObs()}','{$m->getLocal()}','{$m->getExp()}')";
			$res = $this->getDb()->query($sql);
			return ($res !== false ? true : false);
		}

		public function getMovMs($id){
			$sql = "select mo.MOV_MS, mo.MOV, mo.MS, mo.QTDE, mo.TIPO, mo.NF, mo.PED, mo.CLIEN_NOME, mo.NF, mo.DT_OCORRENCIA, mo.OBS, mo.LOCAL, mo.EXPEDICAO
					from mov_ms mo 
					where mov_ms = {$id}";
			$res = $this->getDb()->query($sql);

			if($res){
				$r = $res[0];

				$msdao = new MsDAO($this->getDb());				
				$modao = new MovDAO($this->getDb());
				$mvdao = new msMovDAO($this->getDb());

				$mo = $modao->getMov($r['MOV']);			
				$ms = $msdao->getMs($r['MS']);
				$mv = $mvdao->getMsMov($r['MS']);

				$mm = new MovMs($r['MOV_MS'],$mo,$ms,$r['QTDE'],$r['TIPO'],$r['CLIEN_NOME'],$r['PED'],$r['NF'],$r['DT_OCORRENCIA'],$r['OBS'],$r['LOCAL'],$r['EXPEDICAO']);

				return $mm;
			}
			return false;	
		}

		public function getLista($where=array()){
			$lista = array();
			$where = ($where ? "where ".(implode(" and ",$where)) : "");

			$sql = "select m.DT, mo.MOV_MS, mo.MOV, mo.MS, mo.QTDE, mo.TIPO, mo.NF, mo.PED, mo.CLIEN_NOME, mo.NF, mo.DT_OCORRENCIA, mo.OBS, mo.LOCAL, mo.EXPEDICAO
					from mov_ms mo
					join mov m on m.mov = mo.mov
					{$where}
					order by m.dt, m.mov, mo.ms";
			
			$res = $this->getDb()->query($sql);
			if($res){
				$msdao = new MsDAO($this->getDb());				
				$modao = new MovDAO($this->getDb());
				$mvdao = new msMovDAO($this->getDb());

				foreach($res as $r){
					$mo = $modao->getMov($r['MOV']);					
					$ms = $msdao->getMs($r['MS']);
					$mv = $mvdao->getMsMov($r['MS']);

					$lista[] = new MovMs($r['MOV_MS'],$mo,$ms,$r['QTDE'],$r['TIPO'],$r['CLIEN_NOME'],$r['PED'],$r['NF'],$r['DT_OCORRENCIA'],$r['OBS'],$r['LOCAL'],$r['EXPEDICAO']);
				}
			}
		
			return $lista;
		}
	}
?>